package cn.school.demo.ui.register

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.navigation.fragment.findNavController
import cn.school.demo.MainViewModel
import cn.school.demo.MyApplication
import cn.school.demo.R
import cn.school.demo.databinding.FragmentFirstRegisterBinding
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils

/**
 * 界面： 注册首界面
 * 说明： 免登录
 */
class RegisterFirstFragment : Fragment() {

    private var _binding: FragmentFirstRegisterBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentFirstRegisterBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        init()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun init() {
        MyApplication.systemBarSetting(MyApplication.DARK, requireActivity())
        val array: Array<String> = MainViewModel().identity
        /*
         * ArrayAdapter初始化时
         * context： activity上下文
         * R.layout.spinner_item：为自己定义的一个xml布局
         * array：数组
         * ==============================================
         * requireActivity() : 返回与当前fragment关联的activity
         */
        val _adapter: ArrayAdapter<String> = ArrayAdapter(
            this.requireActivity(),
            R.layout.support_simple_spinner_dropdown_item,
            array
        )
        _adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item)

        with(binding.registerIdentitySpinner) {
            //prompt = "请选择身份" //标题栏
            adapter = _adapter //设置adapter
            setSelection(0)   //设置默认选中选项
            //设置选项点击事件
            onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                override fun onItemSelected(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long
                ) {
                    LogUtils.i("选中了${array[position]}")
                    when (array[position]) {
                        "教职工" -> {
                            findNavController().navigate(R.id.action_Register_to_RegisterByTeacher)
                            setSelection(0)
                        }
                        "学生" -> {
                            findNavController().navigate(R.id.action_Register_to_RegisterByStudent)
                            setSelection(0)
                        }
                        else -> {}
                    }

                }

                override fun onNothingSelected(parent: AdapterView<*>?) {
                    ToastUtils.showShort("not implemented")
                }
            }
        }
    }
}